#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>

void generate_and_write_file(const char *file_name){
	char *hint = access(file_name, F_OK) ? "文件不存在" : "文件存在";
	printf("%s %s\n", file_name, hint);
	FILE* file = fopen(file_name, "w+");
	if(!file){
		perror("generate file failed: ");
		exit(-1);
	}
	for (int i = 0; i < 100; i++){
		fprintf(file, "current line(中文): %d\n", i + 1);
	}
	fclose(file);
}

void read_and_print_file_content(const char *file_name){
	FILE* file = fopen(file_name, "r");
	if (!file){
		perror("open file fialed: ");
		exit(-2);
	}
	char* line = NULL;
	size_t line_len = 0;
	ssize_t read_len;
	while ((read_len = getline(&line, &line_len, file)) != -1){
		printf("read_len: %zd, %s", read_len, line);
		printf("current_size: %ld.\n", ftell(file));
	}
	if (line){
		free(line);
	}
	fclose(file);
}

int main(int argc, char** argv) {
	const char *file_name = "file_demo_test_file.txt";
	generate_and_write_file(file_name);
	read_and_print_file_content(file_name);
}
